package visualization.renderers
{
	import mx.controls.Label;
	import mx.core.UIComponent;
	
	import visualization.IDataPoint;
	import visualization.Visualization;
	import visualization.renderers.utils.DrawingUtils;
	
	public class PieGraph extends VisualizationRenderer
	{
		public var weightField:Object = 0;
		public var elementSize:Object = 20;
		
		public function PieGraph()
		{
			super();
		}
		
		private var _thetaPerUnit:Number = 0;
		private var _centerX:Number;
		private var _centerY:Number;
		private var _radius:Number;
		override public function renderFull(source:Visualization):void{
			if(this.weightField == null) return;
			source.resetLabels();
			source.background.graphics.clear();
			
			var theta:Number = 0;
			var datapoint:IDataPoint;
			for each(var item:Object in this.getLayout(source)){
				var step:Number = this._thetaPerUnit*item.weight;
				source.background.graphics.beginFill(item.color);
				DrawingUtils.drawArc(source.background,this._centerX,this._centerY,this._radius,step,theta);
				if(!(item.item is Visualization)){
					var lab:Label = (item.item as IDataPoint).getLabel();
					lab.text = item.text;
					lab.x = 0;
					lab.y = 0;
				}
				
				datapoint = item.item as IDataPoint;
				datapoint.visible = true;
				datapoint.x = item.x;
				datapoint.y = item.y;
				datapoint.width = item.width;
				datapoint.height = item.height;
				
				theta += step;
			}
		}
		
		override public function renderThumbnail(source:Visualization):void{
			if(this.weightField == null) return;
			source.resetLabels();
			source.background.graphics.clear();
			
			var theta:Number = 0;
			var datapoint:IDataPoint;
			for each(var item:Object in this.getLayout(source)){
				var step:Number = this._thetaPerUnit*item.weight;
				source.background.graphics.beginFill(item.color);
				DrawingUtils.drawArc(source.background,this._centerX,this._centerY,this._radius,step,theta);
				theta += step;
				(item.item as UIComponent).visible = false;
			}
		}
		
		override public function getLayout(source:Visualization):Array{
			var layout:Array = new Array();
			var totalWeight:Number = 0;
			for each(var item:IDataPoint in source.dataPoints){
				var w:Number = source.getValue(this.weightField,item) as Number;
				layout.push({ 
					item:item, 
					x:0, y:0, 
					weight:w, 
					color:(source.getValue(this.elementColor,item) as Number), 
					text:source.getValue(this.elementText,item)});
				totalWeight += w;
			}
			this._thetaPerUnit = (2*Math.PI) / totalWeight;
			var theta:Number = 0;
			this._centerX = source.width/2;
			this._centerY = source.height/2;
			this._radius = (this._centerX<this._centerY)?this._centerX:this._centerY;
			for each(var point:Object in layout){
				var step:Number = this._thetaPerUnit * point.weight;
				
				// Placement of sub-elements
				var cx:Number = this._centerX + this._radius/2 * Math.cos(theta + step/2);
				var cy:Number = this._centerY + this._radius/2 * Math.sin(theta + step/2);
				var ax:Number = this._centerX + this._radius/2 * Math.cos(theta + step);
				var ay:Number = this._centerY + this._radius/2 * Math.sin(theta + step);
				var bx:Number = this._centerX + this._radius/2 * Math.cos(theta);
				var by:Number = this._centerY + this._radius/2 * Math.sin(theta);
				var side:Number = source.getValue(this.elementSize, point.item) as Number;
				
				point.x = cx-(side/2);
				point.y = cy-(side/2);
				point.height = side;
				point.width = side;
				
				theta += step;
			}
			return layout;
		}
		
		override public function clone():VisualizationRenderer{
			var vis:PieGraph = new PieGraph();
			this.initClone(vis);
			return vis;
		}
		
		override protected function initClone(rend:VisualizationRenderer):void{
			var vis:PieGraph = rend as PieGraph;
			vis.weightField = this.weightField;
			super.initClone(vis);
		}
	}
}